package com.djsearch.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * 图片处理类
 * @author chenyang2
 *
 */
public class ImageManager {
	
	public final static String PHOTO_PATH = "/mnt/sdcard/DJsearch/";
	/**
	 * 将drawable 转换为bitmap
	 * @param drawable
	 * @return
	 */
//	public static Bitmap drawableToBitmap(Drawable drawable,int scale){ 
//		int width = drawable.getIntrinsicWidth(); 
//		int height = drawable.getIntrinsicHeight(); 
//		Bitmap bitmap = Bitmap.createBitmap(width, height, 
//		drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
//		: Bitmap.Config.RGB_565); 
//		Canvas canvas = new Canvas(bitmap); 
//		drawable.setBounds(0,0,width,height); 
//		drawable.draw(canvas); 
//		if (scale == 0) {
//			return bitmap;
//		} else {
//			Bitmap newBitmap = resizeImage(bitmap, scale);
//		    return newBitmap; 
//		}
//		} 
	
	public static Bitmap drawableToBitmap(Drawable drawable){ 
		int width = drawable.getIntrinsicWidth(); 
		int height = drawable.getIntrinsicHeight(); 
		Bitmap bitmap = Bitmap.createBitmap(width, height, 
		drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
		: Bitmap.Config.RGB_565); 
		Canvas canvas = new Canvas(bitmap); 
		drawable.setBounds(0,0,width,height); 
		drawable.draw(canvas); 
		return bitmap; 
		} 
	
	public static Bitmap drawableToBitmap(Drawable drawable,float scaleLength){ 
		int width = drawable.getIntrinsicWidth(); 
		int height = drawable.getIntrinsicHeight(); 
		Bitmap bitmap = Bitmap.createBitmap(width, height, 
		drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
		: Bitmap.Config.RGB_565); 
		Canvas canvas = new Canvas(bitmap); 
		drawable.setBounds(0,0,width,height); 
		drawable.draw(canvas); 
		Bitmap newBitmap = resizeImage(bitmap, scaleLength);
		return newBitmap; 

		}
	/**
	 * 将 bitmap转换为drawable
	 * @param bitmap
	 * @return
	 */
	public static Drawable BitmapConvertToDrawale(Bitmap bitmap) {

	// Bitmap bitmap = new Bitmap();
	Drawable drawable = new BitmapDrawable(bitmap);
	return drawable;
	}

	
	// 缩放图片
	public static Bitmap resizeImage(Bitmap bitmap, float scaleLength) {
			int widthBitmap = bitmap.getWidth();
		    float scale = (float) (scaleLength/widthBitmap);
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            return resizedBitmap;
   }
	
	/**
	 * 保存图片到 sd卡
	 * @param path
	 * @param bitmap
	 */
	public static void writeImgToSDcard(String path,String fileNameUrl,Bitmap bitmap) {
		
		if (!isSDcardExist()) {
			
			return ;
			
		} else {
			FileOutputStream fos = null;
		       try {
		    	   //创建目录
		           File sddir = new File(PHOTO_PATH + path);
		           if (!sddir.exists()) {
		              sddir.mkdirs();
		           }
		           fos = new FileOutputStream(sddir.getAbsolutePath() + File.separator + 
		        		   resolveFileNameUrl(fileNameUrl));
		           if (fos != null) {
		        	   if (bitmap != null) {
		        		    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
		        	   }
		              fos.close();
		           }
		} catch (Exception e) {
			// TODO: handle exception
		} 
		}
	}
	
	/**
	 * 处理图片url地址为保存文件名
	 * @param fileName
	 * @return
	 */
	private static String resolveFileNameUrl (String fileNameUrl) {
		int pos = fileNameUrl.lastIndexOf("/");
		String picStr = fileNameUrl.substring(pos+1, fileNameUrl.length());
		return picStr;
	}
	
	/**
	 * 判断sd卡是否存在
	 * @return
	 */
	public static boolean isSDcardExist() {
		
		if (android.os.Environment.getExternalStorageState().equals(  
				    android.os.Environment.MEDIA_MOUNTED)) { 
			return true;
			
		}  else {
			return false;
		}
				   
	}
	
	/**
	 * 从sd卡读取
	 * @return
	 */
	public static Bitmap readImgFromSDcard(String path,String fileNameUrl) {
		
		if (!isSDcardExist()) {
			
			return null ;
			
		} else {
			
			BitmapFactory.Options localOptions = new BitmapFactory.Options();
			localOptions.inPreferredConfig = (Bitmap.Config.RGB_565);
			localOptions.inPurgeable = true;
			localOptions.inInputShareable = true;
			try {
				File localObject = new File(PHOTO_PATH + path + File.separator + 
						resolveFileNameUrl(fileNameUrl));
				return BitmapFactory.decodeStream(new FileInputStream((File)localObject), 
						null, localOptions);
			 } catch (FileNotFoundException localFileNotFoundException) {
			      localFileNotFoundException.printStackTrace();		   
			 }
		}
		return null;
		
	}
}
